package fastjson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Package: fastjson
 * @ClassName: UseTypeReference
 * @Author: lujieni
 * @Description: 1
 * @Date: 2021-05-14 09:44
 * @Version: 1.0
 */
@Slf4j
public class UseTypeReference {

    /**
     * @Description: fastJson的TypeReference的使用
     * @param
     * @return:
     * @Author: lujieni
     * @Date: 2021/5/14
     */
    @Test
    public void test01(){
        String provicne = "[{'name':'上海市','code':1},{'name':'湖南省','code':2}]";
        List<Province> provinces = JSON.parseObject(provicne, new TypeReference<List<Province>>(){});
        log.info("provinces:"+provinces);
    }

    @Test
    public void test02(){
        String provicne = "[{'name':'上海市','code':1},{'name':'湖南省','code':2}]";
        List<Province> list = JSONArray.parseArray(provicne, Province.class);
        log.info("provinces:"+list.toString());
    }

    /**
     * @JSONField(name="")可以指定json化的时候key的名字
     */
    @Test
    public void test03(){
        Province provicne = new Province();
        provicne.setName("上海");
        provicne.setCode("01");
        DTO dto = new DTO();
        dto.setDATA(Arrays.asList(provicne));
        System.out.println(JSON.toJSONString(dto));
    }

    /**
     * new TypeReference 真牛逼,POJO和List<POJO>都可以转
     */
    @Test
    public void test04(){
        String provicne = "{\"name\":\"上海市\",\"code\":1}";
        Province province = JSON.parseObject(provicne, new TypeReference<Province>(){});
        System.out.println(province.toString());
    }

    /**
     * JSON.toJSONString(father) 即使是多态子类的属性也会打印出来的,这点要放心
     */
    @Test
    public void test05(){
        Son son = new Son();
        son.setHobby("sleep");
        son.setName("儿子");
        Father father = son;
        System.out.println(JSON.toJSONString(father));
    }

    /**
     * 任何时候不推荐java的字段名使用is开头
     * 用is开头fastjson序列化的时候会调用!!!
     */
    @Test
    public void test06(){
        Waybill waybill = new Waybill();
        System.out.println(JSON.toJSONString(waybill));
    }

}

class Waybill{

    public boolean isNO(){
        int n = 5 / 0;
        return true;
    }

}


class Province{

    private String code;

    private String name;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Province{" +
                "code='" + code + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

class DTO{

    @JSONField(name = "fuck")
    List<Province> DATA;

    public List<Province> getDATA() {
        return DATA;
    }

    public void setDATA(List<Province> DATA) {
        this.DATA = DATA;
    }
}

class Father{
    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class Son extends Father{

    private String hobby;

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
}